package alijk.dataView.utils;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import flexjson.JSONDeserializer;
/**
 * 天气及地区
 * 2014年11月25日下午9:40:29
 * @author CheWenliang
 */
public class WeatherUtils {
	public static final Logger logger = LoggerFactory
			.getLogger(WeatherUtils.class);
					
	/**
	 * 通过城市得天气
	 * @param city
	 * @return
	 * @throws Exception 
	 */
	@SuppressWarnings("unchecked")
	public static String getTemp(String city) throws Exception{
		//http://api.map.baidu.com/geocoder?location=30.990998,103.645966&output=json&key=28bcdd84fae25699606ffad27f8da77b
		String url_w = Constants.weather_url.replaceAll(":city", city);
		JSONDeserializer<Map<String,Object>> json = new JSONDeserializer<Map<String,Object>>();
		StringBuffer weatherBuffer = new StringBuffer();
		URL url = new   URL( url_w);
		URLConnection uc = url.openConnection(); 
		InputStream is = uc.getInputStream(); 
		BufferedReader reader = new BufferedReader(new InputStreamReader(is));
		StringBuffer stringBuffer = new StringBuffer();
		String line = null;
		while ((line = reader.readLine()) != null) {
			stringBuffer.append(line);
		}
		Map<String,Object> map = json.deserialize(stringBuffer.toString());
		List<Map<String,Object>> list_result = (List<Map<String, Object>>) map.get("results");
		if(list_result == null){
			return "小主,小的找不到此城市";
		}
		Map<String,Object> map_result = list_result.get(0);
		
		weatherBuffer.append((String) map_result.get("currentCity")+"\n");
		
		List<Map<String,Object>> list_we = (List<Map<String, Object>>) map_result.get("weather_data");
		//得未来几天的天气
		for(int i=0;i<list_we.size();i++){
			Map<String,Object> wether = list_we.get(i);
			String date = wether.get("date").toString();
			String weather_t =  wether.get("weather").toString();
			String temperature = wether.get("temperature").toString();
			String wind = wether.get("wind").toString();
			weatherBuffer.append(date+",");
			weatherBuffer.append(weather_t+"  ");
			weatherBuffer.append(wind+"  ");
			weatherBuffer.append(temperature+"  ");
			weatherBuffer.append("\n");
		}
		weatherBuffer.append("亲,天冷就要多穿衣服哦");
		logger.info("天气："+weatherBuffer.toString());
		return weatherBuffer.toString();
	}
	/**
	 * 通过经纬度得城市
	 * @param lat
	 * @param lng
	 * @return
	 * @throws Exception 
	 */
	@SuppressWarnings("unchecked")
	public static String getLocationCity(String lat,String lng) throws Exception{
		String url_location = Constants.loction_url.replaceAll(":LAT", lat).replaceAll(":LNG", lng);
		JSONDeserializer<Map<String,Object>> json = new JSONDeserializer<Map<String,Object>>();
		String city = "";
		URL url = new   URL( url_location);
		URLConnection uc = url.openConnection(); 
		InputStream is = uc.getInputStream(); 
		BufferedReader reader = new BufferedReader(new InputStreamReader(is));
		StringBuffer stringBuffer = new StringBuffer();
		String line = null;
		while ((line = reader.readLine()) != null) {
			stringBuffer.append(line);
		}
		//反序列化
		Map<String,Object> map = json.deserialize(stringBuffer.toString());
		Map<String,Object> result = (Map<String, Object>) map.get("result");
		Map<String,Object> addressComponent = (Map<String, Object>) result.get("addressComponent");
		city = addressComponent.get("city").toString();
		logger.info("从百度api得到的城市："+city);
		return city;
	}
	
	public static String getGuanZhuMessage(){
		StringBuffer stringBuffer = new StringBuffer();
		stringBuffer.append("大侠,你是个好人,多谢关注!\n");
		stringBuffer.append("以下为功能简介:\n");
		stringBuffer.append("1、回复 任意信息看笑话.\n");
		stringBuffer.append("2、发送 位置 给我,看您所在城市天气.\n");
		stringBuffer.append("3、回复 天气@城市 看天气,如天气@北京.\n");
		stringBuffer.append("4、回复 车文亮 看作者.\n");
		stringBuffer.append("5、有bug要多多提出,多谢.\n");
		return stringBuffer.toString();
	}
	
	public static void main(String[] args) throws Exception {
		System.out.println(getLocationCity("40.058065","116.455106"));
		System.out.println(getTemp("北京"));
		System.out.println(getGuanZhuMessage());
	}
}
